# rootfs global info
WORKDIR:=$(shell pwd)
ROOTFS_BIN_DIR ?= $(WORKDIR)/rootfs/rootfs/usr/bin
ROOTFS_LIB_DIR ?= $(WORKDIR)/rootfs/rootfs/usr/lib
ROOTFS_SBIN_DIR ?= $(WORKDIR)/rootfs/rootfs/sbin
ROOTFS_CGI_DIR ?= $(WORKDIR)/rootfs/rootfs/usr/www/cgi-bin
MAKE:=make
CP:=cp -f
RM:=rm -rf

export WORKDIR ROOTFS_BIN_DIR ROOTFS_SBIN_DIR ROOTFS_CGI_DIR MAKE CP RM ROOTFS_LIB_DIR 

# libs and their include dir
PLAT_LIB_DIR := $(WORKDIR)/libplat/lib
PLAT_INC_DIR := $(WORKDIR)/libplat/include
PLAT_INNER_INC := $(WORKDIR)/libplat/src/include

MPI_LIB_DIR := $(WORKDIR)/libmpi/lib
MPI_INC_DIR := $(WORKDIR)/libmpi/include

APP_LIB_DIR := $(WORKDIR)/libapp/lib
APP_INC_DIR := $(WORKDIR)/libapp/include
RTSP_LIB_DIR := $(WORKDIR)/libapp/lib/rtsp_lib
RTSP_SHARED_LIB_DIR := $(WORKDIR)/libapp/lib/rtsp_shared_lib

export PLAT_LIB_DIR PLAT_INC_DIR PLAT_INNER_INC
export MPI_LIB_DIR MPI_INC_DIR
export APP_LIB_DIR APP_INC_DIR RTSP_LIB_DIR RTSP_SHARED_LIB_DIR

# compile toolchain
CROSS_COMPILE ?= arm-anykav200-linux-uclibcgnueabi-
CC := $(CROSS_COMPILE)gcc
LD := $(CC)
STRIP :=$(CROSS_COMPILE)strip

DEBUG := n
ifeq ($(DEBUG), y)
	CFLAGS := -g -O0
	CXXFLAGS := -g -O0
else
	CFLAGS := -fno-strict-aliasing -Os
	CXXFLAGS := -fno-strict-aliasing -Os
endif 

CFLAGS += -Wall -Werror -D_GNU_SOURCE -std=c99 -fms-extensions

STD_LDFLAGS = -lrt -lpthread -ldl -Wl,-rpath=/mnt -rdynamic

AR := $(CROSS_COMPILE)ar
ARFLAGS := crs
export CROSS_COMPILE CC LD STRIP CFLAGS CXXFLAGS AR ARFLAGS STD_LDFLAGS DEBUG 

PLAT_RELEASE := y

# config compile lib type
ifeq ("$(origin SO)", "command line")
	COMPILE_SO := y
	LOCAL_COMMON_FLAG := -shared -fPIC -o
	LOCAL_GEN := $(CC)
else
	COMPILE_SO := n
	LOCAL_COMMON_FLAG := $(ARFLAGS)
	LOCAL_GEN := $(AR)
endif
export PLAT_RELEASE COMPILE_SO LOCAL_COMMON_FLAG LOCAL_GEN

# rootfs dir MUST be placed at first
dir-y := rootfs 
dir-y += libplat
dir-y += libmpi
dir-y += libapp
dir-y += apps

all:
	@for i in $(dir-y); \
	do \
		$(MAKE) -C $$i; \
		echo ""; \
		if [ $$? -ne 0 ];then exit 1; fi \
	done

install:
	@for i in $(dir-y);	\
	do	\
		$(MAKE) -C $$i install;	\
		echo ""; \
	done

image:
	@for i in $(dir-y);	\
	do	\
		$(MAKE) -C $$i image; \
	done

nandimage:
	@for i in $(dir-y);	\
	do	\
		$(MAKE) -C $$i nandimage; \
	done

clean:
	@for i in $(dir-y); \
	do \
		$(MAKE) -C $$i clean; \
		echo ""; \
	done

.PHONY: all install image nandimage clean
